package br.edu.universo.si.projeto2.infojob.client.componentes;

import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * COMPONENTE - Campo para CNPJ
 * @author Andrey A. de Oliveira
 * @since 21/09/2008
 */
public class CNPJBox extends TextBox
{
	public CNPJBox()
	{
		super();
		
		this.setWidth("150px");
		this.setTextAlignment(ALIGN_RIGHT);
		this.setMaxLength(18);
		
		this.addKeyboardListener(
				new KeyboardListener()
				{
					public void onKeyDown(Widget sender, char keyCode, int modifiers)
					{
						if (keyCode != '0' && keyCode != '1' && keyCode != '2' && keyCode != '3' && keyCode != '4' && 
							keyCode != '5' && keyCode != '6' && keyCode != '7' && keyCode != '8' && keyCode != '9' &&
							((int) keyCode) != KEY_BACKSPACE && ((int) keyCode) != KEY_DELETE && ((int) keyCode) != KEY_HOME && ((int) keyCode) != KEY_END 
							&& ((int) keyCode) != KEY_TAB && ((int) keyCode) != KEY_LEFT && ((int) keyCode) != KEY_RIGHT && ((int) keyCode) != KEY_SHIFT)
						{
							((TextBox) sender).cancelKey();
						}
					}

					public void onKeyPress(Widget sender, char keyCode, int modifiers)
					{
						if (keyCode != (char) KEY_BACKSPACE)
						{
							if (CNPJBox.this.getText().length() == 2 || CNPJBox.this.getText().length() == 6)
							{
								CNPJBox.this.setText(CNPJBox.this.getText() + ".");
							}
							
							if (CNPJBox.this.getText().length() == 10)
							{
								CNPJBox.this.setText(CNPJBox.this.getText() + "/");
							}

							if (CNPJBox.this.getText().length() == 15)
							{
								CNPJBox.this.setText(CNPJBox.this.getText() + "-");
							}
						}
					};

					// implementacao vazia...
					public void onKeyUp(Widget sender, char keyCode, int modifiers) { };
				}
			);
		
			// limpa o campo caso o CNPJ nao esteja completo
			this.addFocusListener(
				new FocusListener()
				{
					public void onFocus(Widget sender) { };

					public void onLostFocus(Widget sender)
					{
						if (CNPJBox.this.getText().length() != 18)
						{
							CNPJBox.this.setText("");
						}
					};
				}
			);
			
	}
	
	/**
	 * Verifica se o CNPJ eh valido 
	 */
    public boolean isCNPJValido()
    {
    	// limpa o CNPJ para o processo de validacao...
    	String CNPJ = CNPJBox.this.getText().replace(".", "");
    	CNPJ = CNPJ.replace("/", "");
    	CNPJ = CNPJ.replace("-", "");

    	int soma = 0, dig;
    	String cnpjCalc = CNPJ.substring(0,12);
    	
    	if ( CNPJ.length() != 14 )
    	{
    		return false;
    	}
	
    	char[] chr_cnpj = CNPJ.toCharArray();
	
	   for( int i = 0; i < 4; i++ )
	   {
		   if ( chr_cnpj[i]-48 >=0 && chr_cnpj[i]-48 <=9 )
		   {
			   soma += (chr_cnpj[i] - 48 ) * (6 - (i + 1)) ;
		   }
	   }

	   for( int i = 0; i < 8; i++ )
	   {
		     if ( chr_cnpj[i+4]-48 >=0 && chr_cnpj[i+4]-48 <=9 )
		     {
		    	 soma += (chr_cnpj[i+4] - 48 ) * (10 - (i + 1)) ;
		     }
	   }

	   dig = 11 - (soma % 11);

	   cnpjCalc += ( dig == 10 || dig == 11 ) ? "0" : Integer.toString(dig);
	
	   soma = 0;

	   for ( int i = 0; i < 5; i++ )
	   {
		     if ( chr_cnpj[i]-48 >=0 && chr_cnpj[i]-48 <=9 )
		     {
		    	 soma += (chr_cnpj[i] - 48 ) * (7 - (i + 1)) ;
		     }
	   }

	   for ( int i = 0; i < 8; i++ )
	   {
		     if ( chr_cnpj[i+5]-48 >=0 && chr_cnpj[i+5]-48 <=9 )
		     {
		    	 soma += (chr_cnpj[i+5] - 48 ) * (10 - (i + 1)) ;
		     }
	   }

	   dig = 11 - (soma % 11);

	   cnpjCalc += ( dig == 10 || dig == 11 ) ? "0" : Integer.toString(dig);

	   return CNPJ.equals(cnpjCalc);
    }

	/**
	 * Verifica se o campo esta vazio 
	 */
	public boolean isVazio()
	{
		if (CNPJBox.this.getText().equalsIgnoreCase(""))
		{
			return true;
		}

		return false;
	}
}
